﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using Jy123.DBUtility;
using Jy123.BLL.User;
using Jy123.Model;
using Jy123.Common.Config;
namespace Jy123.BLL.User
{
    public class SPace
    {
        
        /// <summary>
        /// select控件取得选择值
        /// </summary>
        /// <param name="select">选择按钮的HTML空间名</param>
        /// <param name="str">用户数据库中的值</param>
        /// <param name="bl">是否选中值</param>
        public void GetSelect(HtmlSelect select, string str, bool bl)
        {
            for (int i = 0; i < select.Items.Count; i++)
            {
                if (bl)
                {
                    if (str == select.Items[i].Text)
                    {
                        select.Value = select.Items[i].Value;
                        break;
                    }
                }
                else
                {
                    if (str == select.Items[i].Value)
                    {
                        select.Value = select.Items[i].Value;
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="Id">用户ID</param>
        /// <param name="flag">信息类别对应的标签:
        /// 0会员基本信息
        /// 1我的好友信息
        /// 2发表的心情日记
        /// 3会员分享
        /// 4群组信息
        /// 6相册信息
        /// 7根据相册编号读取其封面图
        /// 8根据会员编号读取日志
        /// 9)--读取会员工作信息
        /// 10)--读取会员学校信息
        /// 11)--读取会员联系信息和爱好
        /// 12)--读取会员留言信息
        /// 13)--读取会员个人动态
        /// 14)--根据一级地区读取二级地区
        /// 15)--根据学校编号读取院系信息
        /// 16)--根据日志编号读取日志
        /// 17)--根据分享ID读取分享的日志信息
        /// 18)--根据电影的评论编号读取评论信息
        /// 19)--读取上传的相片
        /// 20)--读取心情日记
        /// 21)--读取分享的相册信息
        /// 22)--读取分享的相片信息
        /// 23)--读取我被别人圈的照片数
        /// 24)--获取头像路径
        /// 25)--读取心情的评论数
        /// 26)--读取推荐的会员
        /// 27)--根据评论编号读取评论信息
        /// 28)--根据相册编号读取相册名称
        /// 29)--根据会员编号读取投票
        /// </param>
        /// <returns>返回对应数据表</returns>
        public DataTable GetUserInfo(int Id, int flag)
        {
            //string sql = null;
            //SqlParameter param = new SqlParameter("@ID", Id);
            SqlParameter[] param = new SqlParameter[13];
            param[0] = new SqlParameter("@ID", SqlDbType.Int);
            param[0].Value = Id;
            param[1] = new SqlParameter("@flag", SqlDbType.Int);
            param[1].Value = flag;
            #region switch (flag)
            // switch (flag)
            // {
            //     case 0://根据会员编号获取会员基本信息
            //         sql = "select a.UserID,a.UserName,a.Click,a.integral,inteyb,Pro=a.Province,citys=a.City,VocName=(select VocName from NT_Dict_Vocation where ID=(select Vocation from NT_UserInfo where UserID=a.UserID)),Province=(select Name from NT_Dict_Area where id=a.Province),City=(select Name from NT_Dict_Area where id=a.City),Sex=(select Sex=case Sex when 0 then '女' when 1 then '男' end from NT_UserInfo where UserID=a.UserID),Birthday=(select Birthday from NT_UserInfo where UserID=a.UserID),Marriage=(select Marriage from NT_UserInfo where UserID=a.UserID),BirthidayDisplay=(select BirthidayDisplay from NT_UserInfo where UserID=a.UserID),Constellation=(select Constellation from NT_Constellation where Id=(select Constellation from NT_UserInfo where UserID=a.UserID)),HomeProv=(select Name from NT_Dict_Area where id=(select HomeProv from NT_UserInfo where UserID=a.UserID)),HomeCity=(select Name from NT_Dict_Area where id=(select HomeCity from NT_UserInfo where UserID=a.UserID)),Photo=(select FilePath from NT_Photo where PhotoID=a.Portrait and PhotoType=0) from NT_User a where a.UserId=@ID";
            //         break;
            //     case 1://根据会员编号读取我的好友信息
            //         sql = "select  a.UserID,a.UserName,a.Portrait,FilePath=(select FilePath from NT_Photo where PhotoID=a.Portrait ) from NT_User a where a.UserID in (select b.FriendID from NT_Friend b where b.UserID=@ID) order by a.LastLoginTime desc";
            //         break;
            //     case 2://根据会员编号读取其发表的心情日记
            //         sql = "select ID,Content,PostTime,(select count(ID) from NT_MoodComment where MiniBlogID=NT_Mood.ID) as MCount from NT_Mood where UserID=@ID and IsLock=0 order by PostTime desc";
            //         break;
            //     case 3://根据会员编号读取会员分享
            //         sql = "select top 5 ID,Num=(select count(*) from NT_Share where UserId=@ID and IsLock=0),Title,Description,ShareType,CaseID,PostTime,Comments,Privacy from NT_Share where UserId=@ID and IsLock=0 order by PostTime desc";
            //         break;
            //     case 4://根据会员编号读取群组信息
            //         sql = "select a.GroupID,a.GroupName,a.UserID,a.Members,a.Portrait from NT_UserGroup a where a.GroupID In(Select c.GroupID From NT_GroupMember as c Where c.UserID=@ID) order by a.PostTime desc";
            //         break;
            //     case 5://根据会员编号读取电影
            //         sql = "select MovieID,Title,Gut,PosterFile from NT_Movie where UserID=@ID order by MovieID desc";
            //         //sql = "select b.UserID,a.MovieID,a.Title,a.PosterFile,b.Score,b.Comment,b.PostTime from NT_Movie a inner join NT_MovieComment b on b.MovieID=a.MovieID where a.UserID=@ID order by b.PostTime desc";
            //         break;
            //     case 6://根据会员编号读取相册信息
            //         sql = "select AlbumID,Title,ImagesCount,LastUploadTime,Privacy from NT_Album where UserID=@ID order by LastUploadTime desc";
            //         break;
            //     case 7://根据相册编号读取其封面图
            //         sql = "select AlbumID,FilePath from NT_Photo where AlbumID=@ID and IsCover=0 and PhotoType=1 and IsLock=0";
            //         break;
            //     case 8://根据会员编号读取日志
            //         sql = "select a.ID,a.Title,a.Content,a.PostTime,Comments=(select count(ID) from NT_BlogComment where BlogID=a.ID ),a.PicPath,a.LastModTime,a.Privacy from NT_Blog a where a.UserID=@ID and a.IsLock=0 order by a.PostTime desc";
            //         break;
            //     case 9://读取会员工作信息
            //         sql = "select ID,Company,Description,IsStill,JoinYear,JoinMonth,LeaveYear,LeaveMonth from NT_UserCareer where UserID=@ID order by PostTime desc";
            //         break;
            //     case 10://读取会员学校信息
            //         sql = "select a.ID,a.UserID,a.DegreeID,a.SchoolID,SchoolName=(select SchoolName from NT_Dict_School where SchoolID=a.SchoolID),Name=(select Name from NT_Dict_Academy where ID=a.AcademyID),a.MatriculationYear,a.AcademyID from NT_UserEducation a where a.UserID=@ID and a.IsLock=0";
            //         break;
            //     case 11://读取会员联系信息和爱好
            //         sql = "select a.Sex,a.Birthday,a.Constellation,a.MSN,a.GTalk,a.QQ,a.Skype,a.Mobile,a.Tel,a.Addr,a.WebSite,a.Favourite,a.FavMusics,a.FavMovies,a.FavCartoons,a.FavGames,a.FavSports,a.FavBooks,a.AppreciateMen,a.FavAdages,a.Presentation,b.Email,a.MobilePrivacy,a.EmailPrivacy,a.MSNPrivacy,a.GTalkPrivacy,a.QQPrivacy,a.SkypePrivacy,a.TelPrivacy,a.AddrPrivacy,a.WebSitePrivacy from NT_UserInfo a inner join NT_User b on a.UserID=b.UserID where a.UserID=@ID";
            //         break;
            //     case 12://读取会员留言信息
            //         sql = "select top 8 a.ID,a.Sender,a.Content,a.PostTime,b.UserName,Province=(select Name from NT_Dict_Area where id=b.Province) from NT_LeaveWord a inner join NT_User b on a.Sender=b.UserID where a.Receiver=@ID and a.IsLock=0 order by a.PostTime desc";
            //         break;
            //     case 13://读取会员个人动态
            //         sql = "select ID,UserID,ActionType,LogTime,Description,Remark,CorrespondInfo,CorreUserID from NT_UserLog where UserID=@ID and isLock=0 and IsDel=0 order by LogTime desc";
            //         break;
            //     case 14://根据一级地区读取二级地区
            //         sql = "select ID,ParentID,Name from NT_Dict_Area where IsLock=0 and ParentID=@ID";
            //         break;
            //     case 15://根据学校编号读取院系信息
            //         sql = "select ID,Name from NT_Dict_Academy where SchoolID=@ID and IsLock=0";
            //         break;
            //     case 16://根据日志编号读取日志
            //         sql = "select ID,Title,Content,Comments,PicPath from NT_Blog where ID=@ID and IsLock=0";
            //         break;
            //     case 17://根据分享ID读取分享的日志信息
            //         sql = "select a.Description,a.Comments,a.Privacy ,b.ID,b.Title,b.Content,b.Comments,b.PicPath from NT_Share a inner join NT_Blog b on a.CaseID=b.ID where a.CaseID=@ID and a.ShareType=4 and b.IsLock=0";
            //         break;
            //     case 18://根据电影的评论编号读取评论信息
            //         sql = "select a.MovieID,a.Title,a.PosterFile,b.Score,b.Comment,b.PostTime from NT_Movie a inner join NT_MovieComment b on b.MovieID=a.MovieID  where b.ID=@ID";
            //         break;
            //     case 19://读取上传的相片
            //         sql = "select a.AlbumID,a.Title,b.FilePath,b.PhotoID from NT_Album a inner join NT_Photo b on a.AlbumID=b.AlbumID where b.PhotoID=@ID";
            //         break;
            //     case 20://读取心情日记
            //         sql = "select ID,Content,Comments from NT_Mood where ID=@ID";
            //         break;
            //     case 21://读取分享的相册信息
            //         sql = "select a.Description,a.Comments,a.Privacy ,b.AlbumID,b.Title,b.ImagesCount from NT_Share a inner join NT_Album b on a.CaseID=b.AlbumID where a.CaseID=@ID and a.ShareType=3";
            //         break;
            //     case 22://读取分享的相片信息
            //         sql = "select a.CaseID,a.Description,a.Comments,a.Privacy ,PhotoDes=b.Description,b.FilePath,c.AlbumID,c.Title from NT_Share a inner join NT_Photo b on b.PhotoID=a.CaseID inner join NT_Album c on b.AlbumID=c.AlbumID where a.CaseID=@ID and a.ShareType=2";
            //         break;
            //     case 23://读取我被别人圈的照片数
            //         sql = "select number=Count(*) from NT_PhotoLasso where FriendID=@ID and IsLock=0"; 
            //         break;
            //     case 24://获取头像路径
            //         sql = "select b.FilePath,b.PhotoID from  NT_Photo b where b.PhotoID=@ID";
            //         break;
            //     case 25://读取心情的评论数
            //         sql = "select ID=Count(ID) from NT_MoodComment where MiniBlogID=@ID";
            //         break;
            //     case 26://读取推荐的会员
            //         sql = "select top 30 UserID from NT_User where Recommended=0 order by RegTime desc";
            //         break;
            //     case 27://根据评论编号读取评论信息
            //         sql = "select a.ID,a.Title from NT_Blog a inner join NT_BlogComment b on a.ID=b.BlogID where b.ID=@ID and a.IsLock=0 order by a.PostTime desc";
            //         break;
            //     case 28://根据相册编号相册名称
            //         sql = "select AlbumID,Title from NT_Album where AlbumID=@ID";
            //         break;
            //}
            #endregion
            DataSet ds = DBUtility.DbHelperSQL.RunProcedure("SP_User_SpaceInfo", param, 0);
            return ds.Tables[0];
        }
        /// <summary>
        /// 获取数据，返回DataTable
        /// </summary>
        /// <param name="flag">读取的类型</param>
        /// <returns>返回下拉表的值列表</returns>
        public DataTable GetSelectTableInfo(int flag)
        {
            string sql = "";
            switch (flag)
            {
                case 0://读取所有星座
                    sql = "select ID,Name=Constellation from User_Constellation";
                    break;
                case 1://读取所有一级地区
                    sql = "select ID,ParentID,Name from User_Dict_Area where IsLock=0 and ParentID=0";
                    break;
                case 2://读取所有行业
                    sql = "select ID,Name=VocName from User_Dict_Vocation where IsLock=0";
                    break;
                case 3://读取所有地区
                    sql = "select ID,ParentID,Name from User_Dict_Area where IsLock=0";
                    break;
            }
            return DBUtility.DbHelperSQL.Query(sql).Tables[0];
        }

        /// <summary>
        /// 添加会员教育信息
        /// </summary>
        /// <param name="modelUserEdu">User_Education实体</param>
        /// <param name="ID">User_Education的ID</param>
        /// <returns></returns>
        public int AddEducationInfo(Jy123.Model.User_Education modelUserEdu, int ID)
        {
            SqlParameter[] param = getEducationParameters(modelUserEdu);
            SqlParameter paramID = new SqlParameter("@ID", ID);
            string sql = "select FDict_School from User_Education where ID=@ID";
            DataTable dt = DbHelperSQL.Query(sql,paramID).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {
                sql = "insert into User_Education(FUser,DegreeID,FDict_School,FDict_Academy,MatriculationYear,PostTime,PostIP,IsLock) values(@UserID,@DegreeID,@SchoolID,@AcademyID,@MatriculationYear,getdate(),@PostIP,0)";
            }
            else
            {
                sql = "update User_Education set FUser=@UserID,DegreeID=@DegreeID,FDict_School=@SchoolID,FDict_Academy=@AcademyID,MatriculationYear=@MatriculationYear,PostTime=getdate(),PostIP=@PostIP where ID=" + ID;
            }
            return DbHelperSQL.ExecuteSql(sql, param);
        }
        ///// <summary>
        ///// 构造函数
        ///// </summary>
        ///// <param name="Info">添加会员教育信息</param>
        ///// <returns></returns>
        /// <summary>
        /// 
        /// </summary>
        /// <param name="modelUserEdu"></param>
        /// <returns></returns>
        private SqlParameter[] getEducationParameters(Jy123.Model.User_Education modelUserEdu)
        {
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[0].Value = modelUserEdu.FUser;
            param[1] = new SqlParameter("@DegreeID", SqlDbType.Int, 4);
            param[1].Value = modelUserEdu.DegreeID;
            param[2] = new SqlParameter("@SchoolID", SqlDbType.Int, 4);
            param[2].Value = modelUserEdu.FDict_School;
            param[3] = new SqlParameter("@AcademyID", SqlDbType.Int, 4);
            param[3].Value = modelUserEdu.FDict_Academy;
            param[4] = new SqlParameter("@MatriculationYear", SqlDbType.Int, 4);
            param[4].Value = modelUserEdu.MatriculationYear;
            param[5] = new SqlParameter("@PostIP", SqlDbType.Char, 15);
            param[5].Value = modelUserEdu.PostIP;
            return param;
        }
        /// <summary>
        /// 根据用户ID判断用户是否有头像,如果没有头像就返回默认头像地址
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>头像地址</returns>
        public string DefaultPortrait(int userID)
        {
            Jy123.Model.User_Main modelUser = new Jy123.Model.User_Main();
            modelUser = new Jy123.BLL.User_Main().GetModel(userID);
            //判断用户是否有头像
            if (string.IsNullOrEmpty(modelUser.Portrait))
            {
                if (new Jy123.BLL.User_Info().GetModel(userID).Sex)
                { modelUser.Portrait = Jy123.Common.Config.BaseConfig.GetConfigValue("PortraitGirl");}
                else { modelUser.Portrait = Jy123.Common.Config.BaseConfig.GetConfigValue("PortraitBoy");}
            }
            return modelUser.Portrait;
        }

        //public string UserPortrait(string dbPortrait)
        //{
        //}
        /// <summary>
        /// 获取用户等级名称
        /// </summary>
        /// <param name="userAllMoney">用户拥有的资产</param>
        /// <returns>等级名称</returns>
        public string UserLevel(int userAllMoney)
        {
            string strSql = "select top(1) LevelName from dbo.User_Level where requirMoney<=@UserAllMoney order by requirmoney desc";
            SqlParameter[] parameters = {
                                        new SqlParameter("@UserAllMoney",SqlDbType.Int)};
            parameters[0].Value = userAllMoney;
            return (string)DBUtility.DbHelperSQL.GetSingle(strSql, parameters);
        }
        /// <summary>
        /// 下一级的资产水平要求
        /// </summary>
        /// <param name="userAllMoney">用户现在拥有的资产</param>
        /// <returns>下一级的资产要求数</returns>
        public int NextLevelMoney(int userAllMoney)
        {
            int requireMoney = 0;
            string strSql = "select  top(1)RequirMoney from dbo.User_Level where requirMoney>@UserAllMoney order by requirmoney asc";
            SqlParameter[] parameters = {
                                        new SqlParameter("@UserAllMoney",SqlDbType.Int)};
            parameters[0].Value = userAllMoney;
            
            requireMoney = Convert.ToInt32(DBUtility.DbHelperSQL.GetSingle(strSql, parameters));
            
            return requireMoney;
        }
    }
}
